{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[B1P147表5-2python表达式运算符及程序]的部分记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x if y else z # 三元选择表达式，仅仅当y为真，x才会被计算\n",
    "\n",
    "x or y # 逻辑或\n",
    "\n",
    "x and y # 逻辑与，这两者都有短路现象\n",
    "\n",
    "not x # 逻辑非"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# Python成员运算符测试给定值是否为序列中的成员，例如字符串，列表或元组。 有两个成员运算符，如下所述 -\n",
    "# in 如果在指定的序列中找到一个变量的值，则返回true，否则返回false。\n",
    "# not in 如果在指定序列中找不到变量的值，则返回true，否则返回false。 \n",
    "a = [1,2,3,4,5]\n",
    "print(2 in a)\n",
    "print(2 not in a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139944745517896 139944745517896\n",
      "a is b?  True\n",
      "a == b?  True\n",
      "------------------------------\n",
      "139944745517896 139944677151688\n",
      "a is b?  False\n",
      "a == b?  True\n"
     ]
    }
   ],
   "source": [
    "# 对象同一性测试，比较地址\n",
    "# 在Python中一切都是对象。Python中对象包含的三个基本要素，\n",
    "# 分别是：id(身份标识)、type(数据类型)和value(值)。对象之间比较是否相等可以用==，也可以用is。\n",
    "# is和 == 都是对对象进行比较判断作用的，但对对象比较判断的内容并不相同。\n",
    "# is比较的是两个对象的id值是否相等，也就是比较两个对象是否为同一个实例对象，是否指向同一个内存地址。\n",
    "# == 比较的是两个对象的内容是否相等，默认会调用对象的__eq__()方法。\n",
    "a = [1,2,3]\n",
    "b = a\n",
    "print(id(a),id(b))\n",
    "print('a is b? ', a is b)\n",
    "print('a == b? ', a == b)\n",
    "b = a[:]\n",
    "print('-' * 30)\n",
    "print(id(a),id(b))\n",
    "print('a is b? ', a is b)\n",
    "print('a == b? ', a == b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 大小比较，链式使用\n",
    "# x < y and y < z 即 x < y < z\n",
    "# 链式比较只计算一次 y，速度更快一些，且允许任意的链式长度[B1P155]\n",
    "print(1 < 2 and 2 < 3)\n",
    "print(1 < 2 < 3)\n",
    "print(1 == 2 < 3) # 即 1 == 2 and 2 < 3，不能按照 C 语言来理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "1\n",
      "128\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 二进制 1：01 二进制3：11\n",
    "x, y = 1, 3\n",
    "print(x | y) # 按位或\n",
    "print(x ^ y) # 按位异或\n",
    "print(x & y) # 按位与\n",
    "print(16 << 3) # 右移 3 位,即乘 2的3次方\n",
    "print(16 >> 3) # 左移 3 位,即除 2的3次方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "2\n",
      "2.0\n",
      "3.0\n",
      "3.0\n",
      "-3\n"
     ]
    }
   ],
   "source": [
    "print(5/2)\n",
    "print(5//2)\n",
    "print(5//2.0) # 向下整除法（地板除），只要有一个为小数结果也会是小数\n",
    "print(6/2) # 经典除法在python3.5中，整除也会保留一位小数。\n",
    "print(6/2.0)\n",
    "print(5//-2) # 不是-2，而是-3，因为取小于-2.5最接近的整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-2\n",
      "-4\n",
      "0b1\n",
      "-0b10\n",
      "0b1\n",
      "-0b10\n"
     ]
    }
   ],
   "source": [
    "# 按位非\n",
    "x, y = 1, 3\n",
    "print(~x)\n",
    "print(~y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1024\n"
     ]
    }
   ],
   "source": [
    "# 幕运算\n",
    "a = 2 ** 10\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数值类型还包括小数、分数、复数和集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'c', 'f', 'd', 'a', 'e'}\n",
      "{'abcdef'}\n",
      "<class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# 集合（set）:不可变对象，无序。一个元素在集合里只会出现一次，无论它被添加了多少次。\n",
    "# 集合是可迭代对象，有点像一个有键无值的字典。\n",
    "# 由于集合是无序的，而且不会把建映射到值，所以它既不是序列也不是映射类型，它是自成一体的类型。[B1P173]\n",
    "# 集合本身可变，但集合里只能包括不可变（可哈希化）的对象类型，因此。列表和字典不能嵌入到集合中，元组可以嵌入。\n",
    "print(set('abcdef')) # 用set()函数将字符串处理成一个集合，\n",
    "print({'abcdef'}) # 这个集合只包含一个元素 ‘abcdef’\n",
    "print(type({'abcdef'}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 7, 8}\n",
      "{1, 2, 3}\n",
      "{8, 6, 7}\n",
      "{4, 5}\n",
      "{1, 2, 3, 6, 7, 8}\n",
      "True False\n",
      "True\n",
      "False False True\n"
     ]
    }
   ],
   "source": [
    "# 集合运算:与数学类似\n",
    "A = set([1,2,3,4,5])\n",
    "B = set([4,5,6,7,8])\n",
    "C = set([1,2,3])\n",
    "D = set([5,6])\n",
    "E = set([5,6])\n",
    "print(A | B) # A 并 B\n",
    "print(A - B) # 差集。A 减 A 交 B，即 A 有但 B 没有。\n",
    "print(B - A)\n",
    "print(A & B) # A 交 B\n",
    "print(A ^ B) # 对称差集：集合的并集减去集合的交集\n",
    "print(C < A, C < B) # 是否包含关系\n",
    "print(B > D)\n",
    "print(D > E, D < E, D == E) # 相等不是包含\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 注意：空集合只能用set()，而不是{}，因为{}代表空字典。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
